/*
 * @Author: ziqi z3ba1233@gmail.com
 * @Date: 2024-08-14 17:37:58
 * @LastEditors: wangdazhuang
 * @LastEditTime: 2024-09-02 11:33:39
 * @FilePath: /pornhub_app/lib/src/views/webview/webview_android.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:pornhub_app/routes/routes.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class AndroidWebViewPage extends StatefulWidget {
  final String url;
  const AndroidWebViewPage({
    super.key,
    required this.url,
  });
  @override
  State<AndroidWebViewPage> createState() => _AndroidWebViewPageState();
}

class _AndroidWebViewPageState extends State<AndroidWebViewPage> {
  late final WebViewController _controller;
  String _title = '';
  @override
  void initState() {
    _controller = WebViewController();
    _controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..addJavaScriptChannel('vip',
          onMessageReceived: (JavaScriptMessage message) {
        Get.toNamed(Routes.vip);
      })
      ..addJavaScriptChannel('wallet',
          onMessageReceived: (JavaScriptMessage message) {
            Get.toVip(tabInitIndex: 1);
      })
      ..setNavigationDelegate(NavigationDelegate(onPageFinished: (url) async {
        _title = await _controller.getTitle() ?? "";
        setState(() {});
      }))
      ..loadRequest(Uri.parse(widget.url));

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_title),
      ),
      body: WebViewWidget(controller: _controller),
    );
  }
}
